package cn.wxxyhw.server;

import cn.wxxyhw.Dao.UserDao;
import cn.wxxyhw.Entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 洪稳
 * @Date: 2019/6/8
 */
@Service
public class UserServer {
    @Autowired
    private UserDao userDao;

    /**
     * 得到全部用户
     * @param name
     * @return
     */
    public List<User> getAll(final String name) {
        List<User> list = userDao.getUsers();
        return list.stream().filter(u -> !u.getUid().equals(name)).collect(Collectors.toList());
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    public User getUserByid(int id) {
        User user = userDao.getUserById(id);
        return user;
    }

    /**
     * 更新信息
     * @param uid  用户登陆id
     * @param nickname 昵称
     * @param email 邮箱
     * @param image 头像
     */
    public void update(String uid, String nickname, String email, String image) {
        if(null!=image){
             image = "/image/"+image;
        }
        userDao.update(uid,nickname,email,image);

        /*更新数据时修改之前的数据*/
        userDao.dataUpdate(uid,nickname,image);
    }

    /**
     * 修改密码
     * @param pw  新密码
     * @param opw 旧密码
     * @param id 用户id
     * @return
     */
    public String updatePassword(String pw, String opw, int id) {
        String old = userDao.getPasswordById(id);
        if (old.equals(opw)){
            userDao.updatePassword(pw,id);
        }else {
            return "error";
        }
        return "success";
    }

    /**
     * 根据Id查找用户信息
     * @param friendId
     * @return
     */
    public User getImageById(String friendId) {
        User user = userDao.getUserByUid(friendId);
        return user;
    }
}
